package com.duan.service.user;

import com.duan.dao.baseDao;
import com.duan.dao.user.UserDao;
import com.duan.dao.user.UserDaoImpl;
import com.duan.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService{
    //业务层都会调用Dao层，所以要引入Dao层
    UserDao userDao;
    public UserServiceImpl(){
        this.userDao = new UserDaoImpl();
    }


    //判断提取的user的password是否和输入一致
    public User loginService(String userCode, String password) {
        Connection connection = null;
        User user = null;

        connection = baseDao.getConnection();
        try{
            //通过业务层调用对应的具体的数据库操作
            user = userDao.loginDao(connection, userCode);
            if(!user.getUserPassword().equals(password)){//如果从数据库提取用户密码和输入密码不一致，就将user = null
                System.out.println("==========================查无此人");
                user = null;
            }

        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            baseDao.closeResource(connection, null, null);
        }

        return user;
    }

    //修改用户密码
    public boolean updatePwd(int id, String password) {
        Connection connection = null;
        boolean flag = false;

        try{
            connection = baseDao.getConnection();
            int res = userDao.updatePwd(connection, id, password);
            if(res > 0){//返回值大于零，成功修改了
                flag = true;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            baseDao.closeResource(connection, null, null);
        }

        return flag;
    }

    //查询记录数
    public int getUserCount(String userName, int userRole) {
        int count = 0;
        Connection connection = null;

        try {
            connection = baseDao.getConnection();
            count = userDao.numOfUser(connection, userName, userRole);

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            baseDao.closeResource(connection, null, null);
        }

        return count;
    }

    //根据条件查询用户列表
    public List<User> getUserList(String userName, int userRole, int currentPageNo, int pageSize) {
        Connection connection = null;
        List<User> userList = null;

        System.out.println("userName---> " + userName);
        System.out.println("userRole---> " + userRole);
        System.out.println("currentPageNo---> " + currentPageNo);
        System.out.println("pageSize---> " + pageSize);

        try{
            connection = baseDao.getConnection();
            userList = userDao.getUserList(connection, userName, userRole, currentPageNo, pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            baseDao.closeResource(connection, null, null);
        }

        return userList;
    }

    //增加新用户
    public boolean insertUser(User user) {
        Connection connection = null;
        boolean flag = false;


        try{
            connection = baseDao.getConnection();
            connection.setAutoCommit(false);//开启JDBC事务管理
            int res = userDao.insertUser(connection, user);

            connection.commit();

            if(res > 0){
                flag = true;
                System.out.println("add success!");
            }else{
                System.out.println("add failed!");
            }
        }catch (SQLException e){
            e.printStackTrace();
            try{
                System.out.println("rollback==================");
                connection.rollback();
            }catch (SQLException e1){
                e1.printStackTrace();
            }
        }finally{
            baseDao.closeResource(connection, null, null);
        }

        return flag;
    }

    //查询指定userCode的用户
    public User selectUserCodeExist(String userCode) {
        Connection connection = null;
        User user = null;
        try{
            connection = baseDao.getConnection();
            user = userDao.selectUserCodeExist(connection, userCode);


        }catch (SQLException e){
            e.printStackTrace();
        }finally{
            baseDao.closeResource(connection, null, null);
        }
        return user;
    }

    //删除用户
    public boolean deleteUser(int userid) {
        Connection connection = null;
        int res = 0;
        boolean flag = false;

        try{
            connection = baseDao.getConnection();
            connection.setAutoCommit(false);
            res = userDao.deleteUser(connection, userid);
            connection.commit();
            if(res > 0){
                flag = true;
            }
        }catch(SQLException e){
            e.printStackTrace();
            try{
                System.out.println("rollback==================");
                connection.rollback();
            }catch (SQLException e1){
                e1.printStackTrace();
            }
        }
        return flag;
    }

    //查询指定id用户信息
    public User selectUserById(int userid) {
        Connection connection = null;
        User user = null;

        try{
            connection = baseDao.getConnection();
            user = userDao.selectUserByid(connection, userid);
        }catch (SQLException e){
            e.printStackTrace();
        }

        return user;
    }

    //修改用户信息
    public boolean modifyUser(User user) {
        Connection connection = null;
        int res = 0;
        boolean flag = false;

        try{
            connection = baseDao.getConnection();
            res = userDao.modify(connection, user);

            if(res > 0){
                flag = true;
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally{
            baseDao.closeResource(connection, null, null);
        }

        return flag;
    }

    //测试service层以下代码是否正确
//    @Test
//    public void Test(){
//        int userCount = getUserCount(null, 0);
//        System.out.println(userCount);
//    }

}


